﻿using UnityEngine;
using System.Collections;
using System.IO;
using System;
using System.Collections.Generic;

public class MapOverview
{

    private static int mapWidth = DataStorage.Instance.GetMapWidth();
    private static int mapHeight = DataStorage.Instance.GetMapHeight();

    private static string[,] tiles = new string[DataStorage.Instance.GetMapWidth(), DataStorage.Instance.GetMapHeight()];

    private static MapOverview _instance = null;
    public static MapOverview Instance
    {
        get
        {
            lock (typeof(MapOverview))
            {
                if (_instance == null)
                {
                    _instance = new MapOverview();
                    Debug.Log(mapWidth);
                    for (int i = 0; i < mapWidth; i++)
                    {
                        for (int j = 0; j < mapHeight; j++)
                        {
                            tiles[i, j] = "0";
                        }
                    }
                }
            }
            return _instance;
        }
    }

    public void MarkMapAsUsed(int x1, int y1, int x2, int y2)
    {
        for (int i = x1; i <= x2; i++) //Da x Wert von links nach rechts größer wird
        {
            for (int j = y1; j >= y2; j--) //Da y Wert von oben nach unten kleiner wird
            {
                tiles[i, j] = "w";
            }
        }
    }

    public void InsertCorners()
    {
        for (int y = tiles.GetLength(1) - 1; y >= 0; y--)
        {
            for (int x = 0; x < tiles.GetLength(0); x++)
            {
                tiles[x, y] = "0";

            }
            //#if !UNITY_ANDROID

            //#endif
        }


        List<Room> rooms = DataStorage.Instance.GetRoomList();
        foreach (Room neighbourRoom in rooms)
        {
            tiles[neighbourRoom.GetXLeft(), neighbourRoom.GetYBottom()] = "w";
            tiles[neighbourRoom.GetXLeft(), neighbourRoom.GetYTop()] = "w";
            tiles[neighbourRoom.GetXRight(), neighbourRoom.GetYBottom()] = "w";
            tiles[neighbourRoom.GetXRight(), neighbourRoom.GetYTop()] = "w";
        }

        for (int y = tiles.GetLength(1) - 1; y >= 0; y--)
        {
            string temp = "";
            for (int x = 0; x < tiles.GetLength(0); x++)
            {
                if (tiles[x, y] == "0")
                    temp += "*";
                else if (tiles[x, y] == "c")
                    temp += "c";
                else if (tiles[x, y] == "?")
                    temp += "?";
                else
                    temp += "w";
            }
            //#if !UNITY_ANDROID
            if (y == mapHeight - 1)
                using (StreamWriter writer = new StreamWriter("important.txt", false))
                {
                    writer.WriteLine(temp);
                }
            else
                using (StreamWriter writer = new StreamWriter("important.txt", true))
                {
                    writer.WriteLine(temp);
                }
            //#endif
        }

    }

    public string[,] GetTileMap()
    {
        return tiles;
    }

    public void PrintMap()
    {
        for (int y = tiles.GetLength(1) - 1; y >= 0; y--)
        {
            string temp = "";
            for (int x = 0; x < tiles.GetLength(0); x++)
            {
                if (tiles[x, y] == "0")
                    temp += "*";
                else if (tiles[x, y] == "c")
                    temp += "c";
                else if (tiles[x, y] == "?")
                    temp += "?";
                else if (tiles[x, y] == "d")
                    temp += "d";
                else
                    temp += "w";
            }
#if !UNITY_ANDROID || UNITY_ANDROID
            if (y == mapHeight - 1)
                using (StreamWriter writer = new StreamWriter("important.txt", false))
                {
                    writer.WriteLine(temp);
                }
            else
                using (StreamWriter writer = new StreamWriter("important.txt", true))
                {
                    writer.WriteLine(temp);
                }
#endif
        }
    }

    public void DeleteUnnecessary()
    {
        List<Room> rooms = DataStorage.Instance.GetRoomList();
        foreach (Room roomElement in rooms)
        {
            for (int x = roomElement.GetXLeft() + 1; x < roomElement.GetXRight(); x++)
            {
                for (int y = roomElement.GetYTop() - 1; y > roomElement.GetYBottom(); y--)
                {
                    tiles[x, y] = "0";
                }
            }
        }
        PrintMap();

    }

    public void SetCenterPoints()
    {
        List<Room> rooms = DataStorage.Instance.GetRoomList();
        foreach (Room roomElement in rooms)
        {
            tiles[roomElement.GetCenter().x, roomElement.GetCenter().y] = "c";
        }
        PrintMap();
    }

    public void UpdateMapWithRoutes()
    {
        List<Edge> edges = DataStorage.Instance.GetEdgeList();
        foreach (Edge edgeBlaaa in edges)
        {
            foreach (Point pointBlaa in edgeBlaaa.GetRoute())
            {
                tiles[pointBlaa.x, pointBlaa.y] = "?";
            }
        }
    }

    public void InsertDoors()
    {
        List<Edge> usedEdges = DataStorage.Instance.GetUsedEdgeList();
        foreach (Edge edge in usedEdges)
        {
            foreach (Point point in edge.GetRoute())
            {
                if (point.y == edge.roomA.GetYTop() && point.x >= edge.roomA.GetXLeft() && point.x <= edge.roomA.GetXRight()) //top of roomA
                {
                    tiles[point.x, point.y] = "d";
                    edge.roomA.SetDoorInRoom(point);
                    edge.roomB.SetDoorInRoom(point);
                    edge.SetDoor(point);
                    break;
                }
                if (point.y == edge.roomA.GetYBottom() && point.x >= edge.roomA.GetXLeft() && point.x <= edge.roomA.GetXRight()) //bottom of roomA

                {
                    tiles[point.x, point.y] = "d";
                    edge.roomA.SetDoorInRoom(point);
                    edge.roomB.SetDoorInRoom(point);
                    edge.SetDoor(point);
                    break;
                }
                if (point.x == edge.roomA.GetXLeft() && point.y >= edge.roomA.GetYBottom() && point.y <= edge.roomA.GetYTop()) //left of roomA
                {
                    tiles[point.x, point.y] = "d";
                    edge.roomA.SetDoorInRoom(point);
                    edge.roomB.SetDoorInRoom(point);
                    edge.SetDoor(point);
                    break;
                }
                if (point.x == edge.roomA.GetXRight() && point.y >= edge.roomA.GetYBottom() && point.y <= edge.roomA.GetYTop()) //right of roomA
                {
                    tiles[point.x, point.y] = "d";
                    edge.roomA.SetDoorInRoom(point);
                    edge.roomB.SetDoorInRoom(point);
                    edge.SetDoor(point);
                    break;
                }
            }
        }
    }
}
